package com.bt.service.impl;
import com.bt.dto.YddAdvimgDTO;
import com.bt.dto.YddAdvimgPageQueryDTO;
import com.bt.mapper.YddAdvimgMapper;
import com.bt.result.PageResult;
import com.bt.service.YddAdvimgService;
import com.bt.vo.YddAdvimgPageQueryVO;
import com.bt.vo.YddAdvimgVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-05-02 17:23
 */
@Slf4j
@Service
public class YddAdvimgServiceImpl implements YddAdvimgService {
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    /**
     * 广告图详情
     *
     * @param id
     * @return
     */
    @Override
    public YddAdvimgVO rowInfo(Integer id) {
        YddAdvimgVO yddAdvimgVO = yddAdvimgMapper.rowInfo(id);
        return yddAdvimgVO;
    }


    /**
     * 广告图列表-分页
     *
     * @param yddAdvimgPageQueryDTO
     * @return
     */
    @Override
    public PageResult list(YddAdvimgPageQueryDTO yddAdvimgPageQueryDTO) {
        //查询ydd_advimgd数据库的总条数
        Integer count = yddAdvimgMapper.getCount();
        //获取分页的起始索引
        Integer start = (yddAdvimgPageQueryDTO.getPage() - 1) * yddAdvimgPageQueryDTO.getLimit();
        //获取每页展示的数量
        Integer length = yddAdvimgPageQueryDTO.getLimit();
        //获取展示的总页数
        Integer totalPages = 0;
        if(length >0) {
            if (count % 10 == 0) {
                totalPages = count / length;
            } else {
                totalPages = (count / length) + 1;
            }
        }

        //获取当前页码
        Integer currentPage = yddAdvimgPageQueryDTO.getPage();
        //查询条件
        String advimgdesc = yddAdvimgPageQueryDTO.getAdvimgdesc();
        String advimgpos = yddAdvimgPageQueryDTO.getAdvimgpos();

        //获取当前页数据集合
        List<YddAdvimgPageQueryVO> yddAdvimgPageQueryVOList = yddAdvimgMapper.list(start, length,advimgdesc,advimgpos);




        PageResult pageResult = PageResult.builder()
                .count(count)
                .totalPages(totalPages)
                .pageSize(length)
                .currentPage(currentPage)
                .data(yddAdvimgPageQueryVOList)
                .build();
        return pageResult;
    }


    /**
     * 广告图修改
     * @param yddAdvimgDTO
     */
    @Override
    public void update(YddAdvimgDTO yddAdvimgDTO) {
        yddAdvimgMapper.update(yddAdvimgDTO);
    }

    /**
     *  广告图删除
     * @param id
     */
    @Override
    public void del(Integer id) {
        yddAdvimgMapper.del(id);
    }

    /**
     * 广告图添加
     * @param yddAdvimgDTO
     */
    @Override
    public void add(YddAdvimgDTO yddAdvimgDTO) {
        yddAdvimgMapper.add(yddAdvimgDTO);
    }
}

